package com.fqdf.framework.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;


import com.fqdf.framework.android.view.listener.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张枭
 *         date 2017/3/23
 * @version V1.0
 */
public abstract class BaseRecycleAdapter<T, Q extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<Q> {


    private List<T> originList = new ArrayList<>();

    private Context mContext;

    private LayoutInflater layoutInflater;

    private RecycleItemClickListener mListener;

    /**
     * 设置点击事件的回调监听
     *
     * @param listener 回调接口
     */
    public void setListener(RecycleItemClickListener listener) {
        mListener = listener;
    }

    public RecycleItemClickListener getListener() {
        return mListener;
    }

    public BaseRecycleAdapter(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        if (mContext instanceof RecycleItemClickListener) {
            mListener = (RecycleItemClickListener) mContext;
        }
    }

    public void setList(List<T> data) {
        if (originList != null) {
            originList = data;
        }
    }

    public void addList(List<T> data) {
        if (originList != null) {
            originList.addAll(data);
        }
    }

    public void clearList() {
        originList.clear();
    }

    @Override
    public int getItemCount() {
        return originList.size();
    }

    public T getItem(int position) {
        return originList.get(position);
    }

    public Context getContext() {
        return mContext;
    }

    public List<T> getOriginList() {
        return originList;
    }

    public void removeItem(int position) {
        originList.remove(position);
    }

    public void addItem(T t) {
        originList.add(t);
    }

    public void addItem(int position, T t) {
        originList.add(position, t);
    }

    public LayoutInflater getLayoutInflater() {
        return layoutInflater;
    }
}
